﻿Imports Microsoft.Win32
Imports HMSLibrary
Imports System.Net.NetworkInformation
Imports System.Threading
Imports NLog
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration
Imports System.Reflection
Imports System.Configuration

Public Class AppCommon
    Public Shared loginInfo As HMSLibrary.LoginInfo
    Public Shared FinYearID As Integer = 0
    Public Shared FinYear As String = String.Empty
    Public Shared LicenseTo As String = String.Empty
    Public Shared db As Database = EnterpriseLibraryContainer.Current.GetInstance(Of Database)()
    Public Shared IsRegsiter As Boolean = False

    Public Shared Sub LogError(ByVal e As Exception)
        Dim logger As Logger = LogManager.GetCurrentClassLogger()
        logger.Error(e.Message & "Stack Trace :" & e.StackTrace)
    End Sub

    Public Shared Sub RegisterMe()
        Try
            Dim nics() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces()
            Dim macadd As String = nics(0).GetPhysicalAddress.ToString

            Dim regKey As RegistryKey

            regKey = My.Computer.Registry.CurrentUser.OpenSubKey("Software\" & My.Application.Info.CompanyName, True)

            'Trial Version information
            Dim ver As String
            Dim ver1 As String
            'If regKey Is Nothing Then
            '    My.Computer.Registry.CurrentUser.OpenSubKey("Software", True)
            '    My.Computer.Registry.CurrentUser.CreateSubKey("Software\" & My.Application.Info.CompanyName)
            '    regKey = Registry.CurrentUser.OpenSubKey("Software\" & My.Application.Info.CompanyName, True)
            '    regKey.SetValue("AppName", My.Application.Info.AssemblyName)

            'End If

            Dim ver2 As Boolean
            Dim ver3 As String
            ver2 = regKey.GetValue("led", False)
            ver3 = regKey.GetValue("lto", "")
            If ver2 = True Then AppCommon.IsRegsiter = True : AppCommon.LicenseTo = ver3 : Exit Sub


            Dim securityObject As New HMS_Security
            Dim tempLAD As String = securityObject.setEncrypt(Date.Now.ToShortDateString)
            ver = regKey.GetValue("RD", "")
            ver1 = regKey.GetValue("LAD", "")
            If Val(securityObject.setDecrypt(ver)) >= 90 Then
                Dim abt As New AboutUsWindow
                abt.ShowDialog()
                If AppCommon.IsRegsiter = False Then
                    CommonMessages.ShowValidationMsg("Please contact software vendor!")
                    Application.Current.Dispatcher.InvokeShutdown()
                End If
            Else
                'If ver = "" Then
                '    regKey.SetValue("RD", securityObject.setEncrypt("1"))
                '    regKey.SetValue("LAD", tempLAD)
                'Else
                If tempLAD <> ver1 Then
                    Dim RemDays As String
                    RemDays = CInt(securityObject.setDecrypt(ver)) + 1
                    RemDays = securityObject.setEncrypt(RemDays)
                    regKey.SetValue("RD", RemDays)
                    regKey.SetValue("LAD", tempLAD)
                End If
                'End If
            End If
            regKey.Close()
        Catch ex As Exception
            LogError(ex)
            End
        End Try
    End Sub

    Public Shared Function GetAppConfigSetting(key As String)
        Dim exePath = Assembly.GetEntryAssembly().Location
        Dim config = ConfigurationManager.OpenExeConfiguration(exePath)
        Return config.AppSettings.Settings(Key).Value
    End Function

End Class


